package com.zzyl.framework.config;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.region.Region;
import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
import com.zzyl.framework.config.properties.HuaWeiIotConfigProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author robin
 * @date 25.5.26 1:22
 */
@Configuration
@RequiredArgsConstructor
public class IotDAConfig {
    
    private final HuaWeiIotConfigProperties huaWeiIotConfigProperties;
    
    @Bean
    public IoTDAClient iotDaClient() {
        // 创建认证
        ICredential auth=new BasicCredentials().withAk(huaWeiIotConfigProperties.getAk())
                                               .withSk(huaWeiIotConfigProperties.getSk())
                                               .withDerivedPredicate(BasicCredentials.DEFAULT_DERIVED_PREDICATE)
                                               .withProjectId(huaWeiIotConfigProperties.getProjectId());
        
        // 创建IoTDAClient实例并初始化
        return IoTDAClient.newBuilder()
                          .withCredential(auth)
                          .withRegion(new Region(huaWeiIotConfigProperties.getRegionId(),huaWeiIotConfigProperties.getEndpoint()))
                          .build();
    }
}